package com.app.fenak;

import com.app.fenak.asynctasks.CheckEmailAsyncTask;
import com.app.fenak.validation.ExpressionValidation;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EmailActivity extends Activity {

	private EditText loginEmail;
	private Button button;
	private SharedPreferences sharedPreferences;
	private Editor editor;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.login);

		loginEmail = (EditText) findViewById(R.id.login_email);
		button = (Button) findViewById(R.id.next_button);
		sharedPreferences = getSharedPreferences(getString(R.string.pref_name),
				Context.MODE_PRIVATE);
		editor = sharedPreferences.edit();

	}

	@Override
	protected void onStart() {

		super.onStart();

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String email = loginEmail.getText().toString();
				if (ExpressionValidation.IsMatch(email,
						ExpressionValidation.EMAIL_PATTERN)) {

					editor.putString(getString(R.string.email_pref), email);
					editor.commit();

					// check on the email whether it exists or not

					progressDialog = ProgressDialog.show(EmailActivity.this,
							"", getString(R.string.check_email_dialog));
					new CheckEmailAsyncTask().execute(EmailActivity.this, email);

				} else {

					Toast.makeText(EmailActivity.this,
							EmailActivity.this.getString(R.string.wrong_email),
							Toast.LENGTH_LONG).show();

				}

			}
		});
	}

	public void dismissProgressDialog() {
		if (this.progressDialog != null) {
			this.progressDialog.dismiss();
		}
	}

}
